<?php

require_once ( 'Gpc/Controller/BaseStatsModel.php' );

/* All the different model classses this class uses */
require_once ( 'Stats.php' );
require_once ( 'Guilds.php' );
require_once ( 'Races.php' );
require_once ( 'Classes.php' );
require_once ( 'Roles.php' );
require_once ( 'RacesHasClasses.php' );

class Admin_ToonController extends Gpc_Controller_BaseStatsModel {

   protected $returnController = 'toon';

   public function init() {

      $randomKey = md5( unique_id() . time() );
      
      $session = Zend_Registry::get( 'session' );
      $this->view->signature = $session->signature = $randomKey;
      
      $this->view->yui_libraries = array ( "utilities", 
            "selector", "json", 
            "logger", 
            "container" );
   }

   public function indexAction() {

      if ( !Zend_Registry::get( 'user' )->hasAuth( 
            'mod_toon_stats' ) ) {
         $this->_redirect( '/' );
      }
      
      $this->view->stats = Stats::getAll( null, 'name' );
      $this->view->guilds = Guilds::getAll( null, 'name' );
      $this->view->classes = Classes::getAll( null, 
            'name' );
      $this->view->roles = Roles::getAll( null, 'name' );
      $this->view->races = Races::getAll( null, 'name' );
   }

   public function statsAction() {

      if ( !Zend_Registry::get( 'user' )->hasAuth( 
            'mod_toon_stats' ) ) {
         $this->_redirect( '/' );
      }
      
      $this->returnAction = 'stats';
      
      $this->view->stats = Stats::getAll( null, 'name' );
   }

   public function guildsAction() {

      if ( !Zend_Registry::get( 'user' )->hasAuth( 
            'mod_toon_guilds' ) ) {
         $this->_redirect( '/' );
      }
      
      $this->returnAction = 'guilds';
      
      $this->view->guilds = Guilds::getAll( null, 'name' );
   }

   public function rolesAction() {

      if ( !Zend_Registry::get( 'user' )->hasAuth( 
            'mod_toon_roles' ) ) {
         $this->_redirect( '/' );
      }
      
      $this->returnAction = 'roles';
      
      $this->view->roles = Roles::getAll( null, 'name' );
   }

   public function raceclassAction() {

      if ( !Zend_Registry::get( 'user' )->hasAuth( 
            'mod_toon_raceclass' ) ) {
         $this->_redirect( '/' );
      }
      
      $this->view->classes = Classes::getAll( null, 
            'name' );
      $this->view->races = Races::getAll( null, 'name' );
      
      // Determin the request method. POST == post back
      if ( $_SERVER['REQUEST_METHOD'] ==
             "POST" ) { // TODO: I know there's a way to do this check with the framework.
               $this->returnAction = 'raceclass';
         
         /*
          * This handles the customized Race <=> Class restriction table on the raceclass page.
          */
         $ids = array ();
         if ( $this->getRequest()->getParam( 
               'checkall' ) ===
                'checkall' ) {
                  foreach ( $this->view->classes as $class ) {
                     foreach ( $this->view->races as $race ) {
                        $ids[] = array ( 
                              $race['id'], 
                              $class['id'] );
               }
            }
         } else {
            foreach ( $this->getRequest()->getParam( 
                  'raceclass' ) as $raceclass ) {
               $ids[] = explode( 
                     ':', 
                     $raceclass );
            }
         }
         
         $raceclass = new RacesHasClasses( );
         $raceclass->delete( null ); // Simplest way -- clear the table and reinsert.
         

         foreach ( $ids as $idList ) {
            $tmp = array ( 
                  'classes_id' => $idList[1], 
                  'races_id' => $idList[0] );
            $raceclass->insert( 
                  $tmp );
         }
         
         $return = array ( 'success' => true );
         $this->_processReturn( $return );
      }
      
      $this->view->classes = Classes::getAll( null, 
            'name' );
      $this->view->races = Races::getAll( null, 'name' );
      $restrictions = RacesHasClasses::getAll();
      
      $this->view->restrictions = array ();
      foreach ( $restrictions as $ids ) {
         $this->view->restrictions[] = $ids['races_id'] .
                ':' .
                $ids['classes_id'];
      }
   }

   protected function _filter() {
   // nothing here .. .. move along.
   }
}
